﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using DBSchemaViewer.BackEnd.Entities;
using DBSchemaViewer.BackEnd.Entities.MetaEntities;

namespace DBSchemaViewer.BackEnd.ModelXML
{
    public class MetadataDAL
    {
        private DataStoreXML _dataStoreXML  = DataStoreXML.Instance;
        //private MetaDataStoreXML _metaDataStoreXML = MetaDataStoreXML.Instance;

        /*
        private const string __strXMLMySQLEngineType     = "MYSQL";
        private const string __strXMLOracleEngineType    = "ORACLE";
        private const string __strXMLSQLServerEngineType = "SQLSERVER";
        */

        /// <summary>
        /// Canvas Size + DB Engine to handle
        /// </summary>
        /// <returns></returns>
        public EntityDocumentSettings RetrieveDocumentSettings()        
        {
            EntityDocumentSettings documentSettings = new EntityDocumentSettings();

            //SettingsBranch
            XElement settingsNode = _dataStoreXML.SettingsBranch;

            if (settingsNode != null)
            {               
                XElement canvasSizeNode = settingsNode.Element("canvassize");
                XElement dbEngineNode   = settingsNode.Element("dbengine");
                if (canvasSizeNode != null)
                {
                    documentSettings.CanvasSize.Width = Convert.ToDouble(canvasSizeNode.Attribute("x").Value);
                    documentSettings.CanvasSize.Height = Convert.ToDouble(canvasSizeNode.Attribute("y").Value);

                    //documentSettings.OLD_DatabeEngineType = MapXMLEngineTypeValueToEnumValue(dbEngineNode.Attribute("type").Value);
                    documentSettings.SelectedDBEngine = dbEngineNode.Attribute("type").Value;
                }
            }


            return documentSettings;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityDocumentSettings"></param>
        public void SetDocumentSettings(EntityDocumentSettings entityDocumentSettings)
        {
            //SettingsBranch
            XElement settingsNode = _dataStoreXML.SettingsBranch;

            if (settingsNode != null)
            {               
                XElement canvasSizeNode = settingsNode.Element("canvassize");
                XElement dbEngineNode   = settingsNode.Element("dbengine");

                canvasSizeNode.Attribute("x").SetValue(entityDocumentSettings.CanvasSize.Width);
                canvasSizeNode.Attribute("y").SetValue(entityDocumentSettings.CanvasSize.Height);
                dbEngineNode.Attribute("type").SetValue(entityDocumentSettings.SelectedDBEngine);
            }
        }
        

        /// <summary>
        /// Dummy mapping, we can not store the enum (we could store the index number).
        /// But better to store a simple string that anyone can understand when browsing
        /// the XML.
        /// </summary>
        /// <param name="engineType"></param>
        /// <returns></returns>
        /*private EntityDocumentSettings.OLD_DB_ENGINE_TYPE MapXMLEngineTypeValueToEnumValue(string engineType)
        {
            EntityDocumentSettings.OLD_DB_ENGINE_TYPE dbEngineType = EntityDocumentSettings.OLD_DB_ENGINE_TYPE.SQLServer;

            switch (engineType)
            {
                case __strXMLMySQLEngineType:
                    dbEngineType = EntityDocumentSettings.OLD_DB_ENGINE_TYPE.MySQL;
                break;

                case __strXMLOracleEngineType:
                    dbEngineType = EntityDocumentSettings.OLD_DB_ENGINE_TYPE.Oracle;
                break;

                case __strXMLSQLServerEngineType:
                    dbEngineType = EntityDocumentSettings.OLD_DB_ENGINE_TYPE.SQLServer;
                break;
            }

            return dbEngineType;
        }*/

        /// <summary>
        /// Map the enum value (db type) to the string that we are using in the XML Model
        /// </summary>
        /// <param name="engineType"></param>
        /// <returns></returns>
        /*private string MapEnumValueToXMLEngineType(EntityDocumentSettings.OLD_DB_ENGINE_TYPE engineType)
        {
            string strEngineType = __strXMLSQLServerEngineType;

            switch (engineType)
            {
                case EntityDocumentSettings.OLD_DB_ENGINE_TYPE.MySQL:
                    strEngineType = __strXMLMySQLEngineType;
                    break;

                case EntityDocumentSettings.OLD_DB_ENGINE_TYPE.Oracle:
                    strEngineType = __strXMLOracleEngineType;
                    break;

                case EntityDocumentSettings.OLD_DB_ENGINE_TYPE.SQLServer:
                    strEngineType = __strXMLSQLServerEngineType;
                break;
            }

            return strEngineType;

        }*/

        /*
        public List<DatabaseEngine> GetAvailableDBEngines()
        {
            List<DatabaseEngine> dbEnginesCollection = null;

            XElement EnginesNode = _metaDataStoreXML.DBEnginesBranch;

            dbEnginesCollection = (from pEngine in EnginesNode.Elements("dbEngine")
                                   select new DatabaseEngine()
                                   {
                                         ID = Convert.ToInt32(pEngine.Attribute("id").Value)
                                       , xmlDescription = pEngine.Element("xmlDescription").Value
                                       , Description = pEngine.Element("description").Value
                                   }

                                  ).ToList<DatabaseEngine>();

            return dbEnginesCollection;
        }
        */

        /*
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBEngineSelected"></param>
        /// <returns></returns>
        public List<DatabaseType> GetAvailableTypes(string DBEngineSelected)
        {
            List<DatabaseType> dbTypesEnumCollection = null;            
            
            XElement TypesNode = _metaDataStoreXML.DBTypesBranch;

            dbTypesEnumCollection = (from pTypes in TypesNode.Elements("type")

                                     select new DatabaseType()
                                    {
                                        DBType = Convert.ToInt32(pTypes.Attribute("id").Value)
                                        ,Length = Convert.ToInt32(pTypes.Element("length").Value)                                        
                                        ,lengthCanBeEditable = Convert.ToBoolean(pTypes.Element("canbeLengthEditable").Value)
                                        ,Description = (from pDescriptions in pTypes.Element("descriptions").Elements("description")
                                                        where pDescriptions.Element("engine").Value == DBEngineSelected
                                                        select pDescriptions.Element("name").Value                                                        
                                                       ).First()
                                    }
                                   ).ToList<DatabaseType>();

            return dbTypesEnumCollection;
        }
         * */
    }
}
